/**
 * @typedef { Object } ContentDispositionResult
 * @property { boolean } [inline]
 * @property { boolean } [attachment]
 * @property { boolean } [form-data]
 * @property { string } [name]
 * @property { string } [filename]
 */

/**
 * @param { string | null | undefined } contentDisposition
 * @return { ContentDispositionResult }
 */
export default function formatContentDisposition(contentDisposition) {
    /** @type { { [key in string]: string | boolean } } */
    const result = {};

    if (contentDisposition) {
        contentDisposition.split(/; */).forEach(str => {
            const list = str.split("=");
            if (list.length === 2) {
                let key = list[0];
                let value = list[1];
                if (value.startsWith("\"") && value.endsWith("\"")) {
                    value = value.slice(1, -1);
                }
                if (key.endsWith("*") && value.startsWith("UTF-8''")) {
                    key = key.slice(0, -1);
                    value = decodeURIComponent(value.slice(7));
                }
                result[key] = value;
            } else {
                result[list[0]] = true;
            }
        });
    }

    return result;
}
